#include "RymcHelper.h"

CRymcHelper::CRymcHelper()
{
	m_timer.Create(1000, CRymcHelper::TimerThread, (unsigned long)this);
}

CRymcHelper::~CRymcHelper()
{
	m_timer.Destroy();
}

int CRymcHelper::ProcessMcCommand(const char *pRequest, char *pResponse)
{
	CHttpHelper httpHelper(pRequest);
	std::string strBody = httpHelper.GetBody();
	if(strBody.empty())
	{
		J_OS::LOGINFO("Empty");
		return J_JSON_UNKOWN;
	}

	json_object *js = json_tokener_parse((char *)strBody.c_str());
	if(is_error(js))
	{
		return J_JSON_UNKOWN;
	}

	int nRet = J_OK;
	int cmd = json_object_get_int(json_object_object_get(js, (char *)"cmd"));
	json_object *ret_obj = json_object_new_object();
	switch(cmd)
	{
	case 101://录像控制
	{
		r_recordcol recordcol;
		js = json_object_object_get(js, (char *)"parm");
		recordcol.act		= json_object_get_int(json_object_object_get(js, (char *)"act"));
		recordcol.resid 	= json_object_get_string(json_object_object_get(js, (char *)"resid"));
		recordcol.ms	= json_object_get_int(json_object_object_get(js, (char *)"ms"));

		nRet = RecordControl(recordcol.resid.c_str(), recordcol.act, recordcol.ms);

		break;
	}
	case 102://DVR历史查询
	{
		json_object_object_add(ret_obj, (char *)"cmd",json_object_new_int(102));

		break;
	}
	case 103://云台控制
	{
		r_cameracol ptc_cntl;
		js = json_object_object_get(js, (char *)"parm");
		ptc_cntl.resid 	= json_object_get_string(json_object_object_get(js, (char *)"resid"));
		ptc_cntl.action		= json_object_get_int(json_object_object_get(js, (char *)"action"));
		ptc_cntl.value	= json_object_get_int(json_object_object_get(js, (char *)"value"));

		nRet = PtzControl(ptc_cntl.resid.c_str(), ptc_cntl.action, ptc_cntl.value);
		//J_OS::LOGINFO("PTZ_CONTROL");
		break;
	}
	case 104://录像通知
	{
		json_object_object_add(ret_obj, (char *)"cmd", json_object_new_int(104));

		//msg.SetBody(json_object_to_json_string(obj));
		break;
	}
	default:
		break;
	}

	if (nRet < 0)
		nRet = 1;

	json_object_object_add(ret_obj, (char *)"rst", json_object_new_int(nRet));
	httpHelper.SetStatue(200);
	httpHelper.SetBody(json_object_to_json_string(ret_obj));
	strcpy(pResponse, httpHelper.GetString());

	return httpHelper.GetLength();
}

int CRymcHelper::PtzControl(const char *pResid, int nCmd, int nParam)
{
	int nRet = J_OK;
	switch(nCmd)
	{
	case 1:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_UP, nParam);
		break;
	case 2:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_DOWN, nParam);
		break;
	case 3:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_LEFT, nParam);
		break;
	case 4:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_RIGHT, nParam);
		break;
	case 5:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_UP_LEFT, nParam);
		break;
	case 6:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_UP_RIGHT, nParam);
		break;
	case 7:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_DOWN_LEFT, nParam);
		break;
	case 8:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_DOWN_RIGHT, nParam);
		break;
	case 9:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_PRE_SET, nParam);
		break;
	case 10:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_PRE_CLR, nParam);
		break;
	case 11:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_GOTO_PRE, nParam);
		break;
	case 12:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_ZOOM_IN, nParam);
		break;
	case 13:
		nRet = m_deviceControl.PtzControl(pResid, JO_PTZ_ZOOM_OUT, nParam);
		break;
	default:
		nRet = J_UNKNOW;
		break;
	}

	return nRet;
}

int CRymcHelper::RecordControl(const char *pResid, int nCmd, int nStreamType)
{
	int nRet = J_OK;
	switch(nCmd)
	{
	case 1:
		TLock(m_locker);
		m_recordVec.push_back(pResid);
		TUnlock(m_locker);
		break;
	case 2:
		nRet = m_deviceControl.StopRecord(pResid);
		break;
	}

	return nRet;
}

void CRymcHelper::OnTimer()
{
	TLock(m_locker);
	int nRet = J_OK;
	RecordVec::iterator it = m_recordVec.begin();
	if (it == m_recordVec.end())
	{
		//m_timer.Destroy();
		sleep(1);
		TUnlock(m_locker);
		return;
	}
	while (true)
	{
		it = m_recordVec.begin();
		for (; it != m_recordVec.end(); it++)
		{
			nRet = m_deviceControl.StartRecord(it->c_str(), 0, NULL);
			if (nRet == J_OK)
			{
				m_recordVec.erase(it);
				break;
			}
		}
		if (it == m_recordVec.end())
		{
			break;
		}
	}
	TUnlock(m_locker);
	//J_OS::LOGINFO("CControlManager::OnTimer()");
}

